package com.icode.dao.impl;

import com.icode.core.dto.order.PageDTO;
import com.icode.core.model.house.*;
import com.icode.core.model.user.Customer;
import com.icode.core.model.user.User;
import com.icode.core.model.user.UserInfo;
import com.icode.dao.RentDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transaction;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-11-7
 * Time: 上午11:09
 * To change this template use File | Settings | File Templates.
 */
@Repository("rentDao")
public class RentDaoImpl extends EntityDaoImpl implements RentDao {
    @Override
    public List<Rent> findMyHouseByStatus(MyHouseStatus toRent, ProductType type) {
        String hql = "from " + Rent.class.getName() + " r where r.myHouse.myHouseStatus=:status and r.myHouse.houseInfo.buildingType.houseBase.productType=:houseType and r.customerInfo.customer is null";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", toRent);
        query.setParameter("houseType", type);
        return query.list();
    }

    @Override
    public List<Rent> findMyHouseByStatusNoType(MyHouseStatus toRent) {
        String hql = "from " + Rent.class.getName() + " r where r.myHouseStatus=:status and  r.customerInfo.customer is null and r.myHouse.myHouseStatus =:myHouseStatus";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", toRent);
        query.setParameter("myHouseStatus",MyHouseStatus.ACTIVITY);
        return query.list();
    }

    @Override
    public List<Rent> findRentsByStatus(MyHouseStatus status, ProductType houseBase, ProductType apartment) {
        String hql = "from " + Rent.class.getName() + " t where t.myHouseStatus=:status and (t.myHouse.houseInfo.buildingType.houseBase.productType=:houseBase or t.myHouse.houseInfo.buildingType.houseBase.productType=:apartment) and t.customerInfo.customer is null and t.myHouse.myHouseStatus =:myHouseStatus ";
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", status);
        query.setParameter("houseBase", houseBase);
        query.setParameter("apartment", apartment);
        query.setParameter("myHouseStatus",MyHouseStatus.ACTIVITY);
        return query.list();
    }

    @Override
    public List<Rent> findMyHouseByParams(MyHouseStatus status, String area, String week, String price, String theme, ProductType houseBase, ProductType apartment) {
        String hql;
        if (week.equals("不限")) {
            hql = "from " + Rent.class.getName() + " a where a.myHouseStatus=:status and (a.myHouse.houseInfo.buildingType.houseBase.productType=:houseBase or a.myHouse.houseInfo.buildingType.houseBase.productType=:apartment) and a.customerInfo.customer is null and a.myHouse.myHouseStatus =:myHouseStatus" + price + theme + area;
        } else {
            hql = "from " + Rent.class.getName() + " a where a.myHouseStatus=:status and (a.myHouse.houseInfo.buildingType.houseBase.productType=:houseBase or a.myHouse.houseInfo.buildingType.houseBase.productType=:apartment) and a.myHouse.houseWeek.name=:week and a.customerInfo.customer is null and a.myHouse.myHouseStatus =:myHouseStatus" + price + theme + area;
        }
        Query query = currentSession().createQuery(hql);
        query.setParameter("status", status);
        if (!week.equals("不限")) {
            query.setParameter("week", week);
        }
        query.setParameter("houseBase", houseBase);
        query.setParameter("apartment", apartment);
        query.setParameter("myHouseStatus",MyHouseStatus.ACTIVITY);

        return query.list();
    }

    @Override
    public List<Rent> findRentAllByPage(String currentPage, String status) {
        String hql;
        Query query;
        if (status.equals("ALL")) {
            hql = "from " + Rent.class.getName();
            query = currentSession().createQuery(hql);

        } else {
            hql = "from " + Rent.class.getName() + " a where a.myHouseStatus=:status ";
            query = currentSession().createQuery(hql);
            switch (status) {
                case "TORENT":
                    query.setParameter("status", MyHouseStatus.TORENT);
                    break;
                case "RENTING":
                    query.setParameter("status", MyHouseStatus.RENTING);
                    break;
                case "RENTED":
                    query.setParameter("status", MyHouseStatus.RENTED);
                    break;
            }
        }
        query.setFirstResult((Integer.parseInt(currentPage) - 1) * PageDTO.pageOfData);
        query.setMaxResults(PageDTO.pageOfData);
        List<Rent> rents = query.list();
        return rents.isEmpty() ? null : rents;
    }

    @Override
    public List<Rent> findRentForHirer(User user) {
        String hql = "from " + Rent.class.getName() + " a where a.myHouse.userInfo.user=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        return query.list();
    }

    @Override
    public List<Rent> findRentForLessee(User user) {
        String hql = "from " + Rent.class.getName() + " a where a.lessee=:user";
        Query query = currentSession().createQuery(hql);
        query.setParameter("user", user);
        return query.list();
    }

    @Override
    public Rent findRentByMyHouse(MyHouse myHouse) {
        String hql = "from " + Rent.class.getName() + " a where a.myHouse=:myHouse";
        Query query = currentSession().createQuery(hql);
        query.setParameter("myHouse",myHouse);
        List<Rent> list = query.list();
        return list.isEmpty()?null:list.get(0);
    }

    @Override
    public List<Rent> findRentByUsername(String username) {
        String hql = "from " + Rent.class.getName() + " a where a.myHouse.userInfo.username=:username";
        Query query = currentSession().createQuery(hql);
        query.setParameter("username", username);
        return query.list();
    }

    @Override
    public List<Rent> findRentByCustomer(String customer) {
        String hql = "from " + Rent.class.getName() + " a where a.customerInfo.customer=:customer";
        Query query = currentSession().createQuery(hql);
        query.setParameter("customer", customer);
        return query.list();
    }
}
